package com.sopt.friends.db;

import com.sopt.friends.db.Constants.FriendsDB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class FriendsDBHelper extends SQLiteOpenHelper {

	static final String TAG = "changdoc";
	
	
	public FriendsDBHelper(Context c) {
		super(c, FriendsDB.DB_NAME, null, FriendsDB.DB_VERSION);
		
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		//회원DB
		String curSql = "CREATE TABLE " + FriendsDB.TABLE_MEMBER + " ("
				+ FriendsDB.MEMBER_ID + " text,"
				+ FriendsDB.GROUP_SEQ + " text,"
				+ FriendsDB.MEMBER_NAME + " text,"
				+ FriendsDB.MEMBER_PHOTO_ID + " text,"
				+ FriendsDB.MEMBER_EMAIL + " text," 
				+ FriendsDB.MEMBER_BIRTHDAY	+ " date,"
				+ FriendsDB.MEMBER_REGDATE + " date,"
				+ FriendsDB.MEMBER_IS_DELETED + " );";

		db.execSQL(curSql);
		
		//그룹DB
		curSql = "CREATE TABLE " + FriendsDB.TABLE_GROUP + " ("
				+ FriendsDB.GROUP_SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT,"
				+ FriendsDB.GROUP_NAME + " text);";

		db.execSQL(curSql);
		
		//전화번호 DB
		curSql = "CREATE TABLE " + FriendsDB.TABLE_PHONE + " ("
		+ FriendsDB.PHONE_SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT,"
		+ FriendsDB.MEMBER_ID + " text,"
		+ FriendsDB.PHONE_NUMBER + " text,"
		+ FriendsDB.PHONE_TYPE + " text);";
	
		db.execSQL(curSql);
		
		//SNS DB
		curSql = "CREATE TABLE " + FriendsDB.TABLE_SNS + " ("
		+ FriendsDB.SNS_SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT,"
		+ FriendsDB.MEMBER_ID + " text,"
		+ FriendsDB.SNS_TWITTER + " text,"
		+ FriendsDB.SNS_ME2DAY + " text,"
		+ FriendsDB.SNS_FACEBOOK + " text);";
		
		db.execSQL(curSql);
		
//		//주소 DB
//		curSql = "CREATE TABLE " + FriendsDB.TABLE_ADDR + " ("
//		+ FriendsDB.ADDR_SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT,"
//		+ FriendsDB.MEMBER_ID + " text,"
//		+ FriendsDB.ADDR_PROVINCE + " text,"
//		+ FriendsDB.ADDR_CITY + " text,"
//		+ FriendsDB.ADDR_BROUGH + " text,"
//		+ FriendsDB.ADDR_TYPE + " text);";
//		
//		db.execSQL(curSql);
		
//		//추가정보 DB
//		curSql = "CREATE TABLE " + FriendsDB.TABLE_ADD_INFO + " ("
//		+ FriendsDB.ADD_INFO_SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT,"
//		+ FriendsDB.MEMBER_ID + " text,"
//		+ FriendsDB.ADD_INFO_CONTENT + " text,"
//		+ FriendsDB.ADD_INFO_TYPE + " text);";
		
//		db.execSQL(curSql);
		
		//채팅 DB
		curSql = "CREATE TABLE " + FriendsDB.TABLE_CHAT + " ("
		+ FriendsDB.CHAT_SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT,"
		+ FriendsDB.PHONE_NUMBER + " text,"
		+ FriendsDB.CHAT_CONTENT + " text,"
		+ FriendsDB.CHAT_MINE + " text,"
		+ FriendsDB.CHAT_REGDATE + " date);";
		
		db.execSQL(curSql);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.v("changdoc","DATABASE RESET TO version:"+newVersion);
		
		// TODO DB버전 다를경우 DROP 후 생성
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_GROUP);
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_MEMBER);
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_PHONE);
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_SNS);
//		db.execSQL("DROP TABLE "+FriendsDB.TABLE_ADDR);
//		db.execSQL("DROP TABLE "+FriendsDB.TABLE_ADD_INFO);
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_CHAT);
		
		onCreate(db);
	}
	
	public void forceRegenerate(SQLiteDatabase db){
		
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_GROUP);
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_MEMBER);
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_PHONE);
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_SNS);
//		db.execSQL("DROP TABLE "+FriendsDB.TABLE_ADDR);
//		db.execSQL("DROP TABLE "+FriendsDB.TABLE_ADD_INFO);
		db.execSQL("DROP TABLE "+FriendsDB.TABLE_CHAT);
		
		onCreate(db);
	}
	

}
